@{
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<div class="layui-inline ">
    仓库编码：
    <div class="layui-inline ">
        <input class="layui-input layui-input-inline" name="code" id="code" />
    </div>
    仓库名称：
    <div class="layui-inline ">
        <input class="layui-input layui-input-inline" name="name" id="name" />
    </div>

    <button type="button" id="btnSearch" class="layui-btn layui-btn-normal">查询</button>
    <button type="button" class="layui-btn  layui-btn-warm">删除</button>
        <button class="layui-btn layui-btn-primary"
                lay-on="test-page">
            <i class="layui-icon">&#xe654;</i> 新增
        </button>
</div>
<table id="warehouseTable" lay-filter="warehouseTable"></table>

<!-- 表格操作按钮模板 -->
<script type="text/html" id="btn">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
    </div>
</script>

<script src="~/libs/layui-v2.5.6/layui/layui.all.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>

<script>
        layui.use(['table', 'form', 'layer'], function(){

             var table = layui.table;

                //弹出层
                var $ = layui.$;
                var layer = layui.layer;
                var util = layui.util;
                var form = layui.form;

                    var table = layui.table,
                        form = layui.form,
                        layer = layui.layer;
                var funtable = function(){
                            table.render({
                                        url:"/WarehouseSetup/GetWarehouseSetup",
                                        where:{
                                            code:$("#code").val(),
                                            name:$("#name").val(),
                                            pageIndex:1,
                                            pageSize:10
                                        },
                                        elem: '#warehouseTable',
                                        cols: [[
                                             { type: 'checkbox', fixed: 'left' },
                                                    {field: 'warehouseCode', title: '仓库编码', width: 120}
                                                    ,{field: 'warehouseName', title: '仓库名称', width: 150}
                                                    ,{field: 'location', title: '位置'}
                                                    ,{field: 'warehouseArea', title: '面积', width: 100}
                                                    ,{field: 'warehouseHead', title: '负责人', width: 100}
                                                    ,{field: 'isFreeze', title: '状态', width: 100, templet: '#freezeStatusTpl'}
                                                    ,{ title: '操作', width: 160, toolbar: '#btn', fixed: 'right' }

                                    ]],
                                    page: true, //是否显示分页
                                    height: 500,
                                    limits: [10, 20, 30],
                                    limit: 10, //每页默认显示的数量
                                    parseData: function(res){
                                        console.log(res);
                                        return{
                                            code: res.code,
                                        count: res.count,
                                        data: res.data
                                        };
                                    },
                                    });
                        }

                         funtable()

                         $("#btnSearch").on("click",function(){
                            funtable()
                        })

             // 监听表格工具条
        table.on('tool(warehouseTable)', function(obj){
                var data = obj.data;
                console.log('当前行数据:', data); // 添加调试日志

                if(obj.event === 'delete'){
                    layer.confirm('确定要删除该客户吗？', function(index){
                        deleCust(data.id);
                        layer.close(index);
                    });
                } else if(obj.event === 'detail'){
                    layer.open({
                        type: 2,
                        title: '修改仓库信息',
                        area: ['800px', '500px'],
                        content: '/WarehouseSetup/Edit',
                        success: function(layero, index){
                            var iframeWindow = window['layui-layer-iframe' + index];
                            // 等待iframe加载完成后再调用子页面方法
                            setTimeout(function() {
                                if (iframeWindow.loadData) {
                                    iframeWindow.loadData(data.id);
                                }
                            }, 500);
                        }
                    });
                }
            });


                    // 删除客户方法
                var deleCust = function(ids){
                    $.ajax({
                url: '/WarehouseSetup/DeleteWarehouseSetup',
                        type: 'post',
                        data: { id: JSON.stringify(ids) },
                        dataType: 'json',
                        success: function (res) {
                            console.log(res)
                            if(res > 0){
                                layer.msg('删除成功');
                                funtable(); // 刷新表格
                            }else{
                                layer.msg(res.msg || '删除失败');
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('删除失败：' + error);
                            console.error("请求失败：" + status + " " + error);
                        }
                    });
                }


                // 显示新增模态框


            // 事件
                util.on('lay-on', {
                  'test-page': function () {
                    layer.open({
                      type: 2,
                      area: ['800px', '500px'], // 宽高
                      content: '/WarehouseSetup/Create',
                    });
                  },
                      success: function () {
                        // 对弹层中的表单进行初始化渲染
                        form.render();
                        // 表单提交事件
                        form.on('submit(demo-login)', function (data) {
                          var field = data.field; // 获取表单字段值
                          // 显示填写结果，仅作演示用
                          layer.alert(JSON.stringify(field), {
                            title: '当前填写的字段值',
                          });
                          // 此处可执行 Ajax 等操作
                          // …
                          return false; // 阻止默认 form 跳转
                        });
                      },
                });


            // 批量删除按钮点击事件
    $('.layui-btn-warm').on('click', function(){
        var checkStatus = table.checkStatus('warehouseTable');
        var data = checkStatus.data;
        if(data.length === 0){
            layer.msg('请选择要删除的数据');
            return;
        }
        layer.confirm('确定删除选中的 '+ data.length +' 条数据吗？', function(index){
            var ids = data.map(item => item.id );
            console.log(ids);
            // deleCust(ids);
            // layer.close(index);
            $.ajax({
            url: '/WarehouseSetup/DeleAll',
            type: 'post',
            data: { id: ids.toString() },
            dataType: 'json',
            success: function (res) {
                console.log(res)
                if(res > 0){
                    layer.msg('删除成功');
                    funtable(); // 刷新表格
                }else{
                    layer.msg(res.msg || '删除失败');
                }
            },
            error: function (xhr, status, error) {
                layer.msg('删除失败：' + error);
                console.error("请求失败：" + status + " " + error);
            }
        });
        });
    });
        });
</script>